//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package net.zoneland.knowledge.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * JSON UTILS.
 * @author zx
 */
public final class JsonUtils {
    private static final ObjectMapper MAPPER = new ObjectMapper().findAndRegisterModules();

    static {
        MAPPER
            .registerModule(new JavaTimeModule());
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);

    private JsonUtils() {
    }

    public static <T> T readValue(final String jsonStr, final Class<T> clazz) throws IOException {
        return MAPPER.readValue(jsonStr, clazz);
    }

    public static <T> T convertValue(final Object jsonStr, final Class<T> clazz) throws IOException {
        return MAPPER.convertValue(jsonStr, clazz);
    }

    public static <T> T readValue(final Map jsonStr, final Class<T> clazz) throws IOException {
        return MAPPER.readValue(MAPPER.writeValueAsString(jsonStr), clazz);
    }

    public static <T> List<T> readValue(final String jsonStr, final Class<? extends Collection> collectionClass, final Class<T> clazz) throws IOException {
        CollectionType listType = MAPPER.getTypeFactory().constructCollectionType(collectionClass, clazz);
        return (List)MAPPER.readValue(jsonStr, listType);
    }

    public static String writeValueAsString(final Object obj) throws JsonProcessingException {
        try {
            return MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            LOGGER.error("writeValueAsString,obj:{}", obj, e);
            return "";
        }
    }
}
